/*
 * Created on 2004-12-29
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package nc.tb.ufo.sheet;
import java.io.Serializable;

import nc.tb.ctl.EditControlCTL;
import nc.tb.cube.model.Plan;
import nc.tb.pub.exception.RepeatlyCheckOutException;
import nc.vo.pub.BusinessException;
/**
 * @author tzj
 *  
 */
public class EditControlDelegate implements Serializable,Cloneable {
	private boolean bCheckedOut = false;
	private long activityMills = 0;
	private long versionMills = 0;
	
	private long lockSystemTimeMills=0;

	/**
	 *  
	 */
	public EditControlDelegate() {
	}
	
	public Object clone(){
		
		try {
			return  super.clone();
		} catch (CloneNotSupportedException e) {
			throw new InternalError();//impossible
		}
		

	}
	public boolean isCheckedOut() {
		return bCheckedOut;
	}
	public void setCheckedOut(boolean checkedOut) {
		bCheckedOut = checkedOut;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see nc.vo.ntb.sheet.IEditController#checkIn()
	 */
	public synchronized void checkIn(Plan so) throws BusinessException {
		// TODO Auto-generated method stub
		EditControlCTL.checkIn(so);
		this.bCheckedOut = false;
	}
	public synchronized void checkInPlanByView(Plan so, String pk_cubeview) throws BusinessException {
		// TODO Auto-generated method stub
		EditControlCTL.checkInPlanByView(so, pk_cubeview);
		this.bCheckedOut = false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nc.vo.ntb.sheet.IEditController#checkIn()
	 */
	public synchronized void checkIn(Plan so, boolean isAutoSave) throws BusinessException {
		// TODO Auto-generated method stub
		EditControlCTL.checkIn(so, isAutoSave);
		this.bCheckedOut = false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nc.vo.ntb.sheet.IEditController#checkOut()
	 */
	public synchronized void checkOut(Plan so) throws BusinessException {
		// TODO Auto-generated method stub
		checkOutAndLock(so);
	}
	public synchronized void checkOutPlanByView(Plan so, String pk_cubeview) throws BusinessException {
		// TODO Auto-generated method stub
		if (so.getPrimaryKey() == null) {
//			bCheckedOut = true;
			return;
		}
		if (!so.isCheckedOut()) {
			EditControlCTL.checkOutPlanByView(so, pk_cubeview);
//			bCheckedOut = true;
		}else{
//			throw new RepeatlyCheckOutException();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nc.vo.ntb.sheet.IEditController#checkOutAndLock()
	 */
	public void checkOutAndLock(Plan so) throws BusinessException {
		// TODO Auto-generated method stub
		if (so.getPrimaryKey() == null) {
//			bCheckedOut = true;
			return;
		}
		if (!so.isCheckedOut()) {
			EditControlCTL.checkOut(so);
//			bCheckedOut = true;
		}else{
//			throw new RepeatlyCheckOutException();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nc.vo.ntb.sheet.IEditController#isCheckedOut()
	 */
	public synchronized boolean isCheckedOut(Plan so) {
		// TODO Auto-generated method stub
		return so.getPrimaryKey() == null || bCheckedOut;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nc.vo.ntb.sheet.IEditController#undoCheckOut()
	 */
	public synchronized void undoCheckOut(Plan so) throws BusinessException {
		// TODO Auto-generated method stub
		if (so.getPrimaryKey() == null) {
//			bCheckedOut = false;
			return;
		}
		if (so.isCheckedOut()) {
			EditControlCTL.undoCheckOut(so);
//			bCheckedOut = false;
		}
	}
	public synchronized void undoCheckOutPlanByView(Plan so, String pk_cubeview) throws BusinessException {
		// TODO Auto-generated method stub
		if (so.getPrimaryKey() == null) {
//			bCheckedOut = false;
			return;
		}
		if (so.isCheckedOut()) {
			EditControlCTL.undoCheckOutPlanByView(so, pk_cubeview);
//			bCheckedOut = false;
		}
	}

	/**
	 * @return Returns the checkedInMills.
	 */
	public long getVersionMills() {
		return versionMills;
	}

	/**
	 * @param checkedInMills
	 *            The checkedInMills to set.
	 */
	public void setVersionMills(long checkedInMills) {
		this.versionMills = checkedInMills;
	}

	/**
	 * @return Returns the checkedOutMills.
	 */
	public long getActivityMills() {
		return activityMills;
	}

	/**
	 * @param checkedOutMills
	 *            The checkedOutMills to set.
	 */
	public void setActivityMills(long activityMills) {
		this.activityMills = activityMills;
	}

	public long getLockSystemTimeMills() {
		return lockSystemTimeMills;
	}

	public void setLockSystemTimeMills(long lockSystemTimeMills) {
		this.lockSystemTimeMills = lockSystemTimeMills;
	}
}